package kids.movies.hoathinhHDSRC.fragment;

import java.util.ArrayList;

import kids.movies.hoathinhHD.R;
import kids.movies.hoathinhHDSRC.BaseFragment;
import kids.movies.hoathinhHDSRC.E4KidsConfig;
import kids.movies.hoathinhHDSRC.adapter.ItemVideoRelativeAdapter;
import kids.movies.hoathinhHDSRC.adapter.ItemVideoRelativeAdapter.IClickItemVideo;
import kids.movies.hoathinhHDSRC.customize.BaseRequestHandler;
import kids.movies.hoathinhHDSRC.db.DbSupport;
import kids.movies.hoathinhHDSRC.db.TableDb;
import kids.movies.hoathinhHDSRC.service.RequestService;
import kids.movies.hoathinhHDSRC.util.Debug;
import kids.movies.hoathinhHDSRC.util.Util;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bzydroid.network.http.NetworkResponse;
import com.bzydroid.network.http.client.BzydroidHttpClient;
import com.bzydroid.network.http.exception.BzydroidNetworkExeception;
import com.bzydroid.network.http.response.HttpJSONObjectResponse;
import com.telpoo.frame.object.BaseObject;
import com.telpoo.frame.utils.JsonSupport;

public class IndexNewFreagment extends BaseFragment {
	private View root;
	private ListView lvVideo;
	private ItemVideoRelativeAdapter adapter;
	private String idCat;
	private LinearLayout llLoading;

	private int page = 1;

	private boolean isLoading = false;

	private RequestVideoNew requestVideoNew;
	private LinearLayout llLoadingFooter;
	private TextView tvNoData;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		root = inflater.inflate(R.layout.layout_list_relative, null);
		return root;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		lvVideo = (ListView) root.findViewById(R.id.lvRelative);

		tvNoData = (TextView) root.findViewById(R.id.tvNoData);

		llLoading = (LinearLayout) root.findViewById(R.id.ll_loading);

		LayoutInflater inflater = getActivity().getLayoutInflater();

		View header = inflater.inflate(R.layout.layout_header, null);

		View footer = inflater.inflate(R.layout.layout_footer, null);

		llLoadingFooter = (LinearLayout) footer
				.findViewById(R.id.llLoadingFooter);

		lvVideo.addHeaderView(header);

		lvVideo.addFooterView(footer);
		adapter = new ItemVideoRelativeAdapter(getActivity(),
				imageLoader, options);
		lvVideo.setAdapter(adapter);
		adapter.setData(DbSupport.getCache(TableDb.NEW));
		idCat=String.valueOf(application.getCatID());

		loadMore();


		adapter.setClickItemVideo(new IClickItemVideo() {

			@Override
			public void onclickItem(final String vid, final String linkvideo) {
				if (vid.equals("4") || vid.equals("8") || vid.equals("14")
						|| vid.equals("20")) {
					if (application.isAds()) {
						application.setAds(false);

					} else {
//						Bundle bundle = new Bundle();
//						bundle.putString(E4KidsConfig.KEY_INTENT_VID, vid);
//						bundle.putString(E4KidsConfig.KEY_INTENT_LINK,
//								linkvideo);
//						gotoActivity(PlayVideoYoutbe.class, bundle);
						Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.youtube.com/watch?v=" +linkvideo));
						startActivity(intent);
					}
				} else {
//					Bundle bundle = new Bundle();
//					bundle.putString(E4KidsConfig.KEY_INTENT_VID, vid);
//					bundle.putString(E4KidsConfig.KEY_INTENT_LINK, linkvideo);
//
//					gotoActivity(PlayVideoYoutbe.class, bundle);
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.youtube.com/watch?v=" +linkvideo));
					startActivity(intent);
				}

			}
		});

		if (lvVideo.isSmoothScrollbarEnabled()) {
			Debug.debug("Keo duoc listview");
		} else {
			Debug.debug("Khong Keo duoc listview");
		}

		adapter.setClickItemVideo(new IClickItemVideo() {

			@Override
			public void onclickItem(final String vid, final String linkvideo) {
				if (vid.equals("3") || vid.equals("10") || vid.equals("15")
						|| vid.equals("19")) {
					if (application.isAds()) {
						application.setAds(false);

					} else {
//						Bundle bundle = new Bundle();
//						bundle.putString(E4KidsConfig.KEY_INTENT_VID, vid);
//						bundle.putString(E4KidsConfig.KEY_INTENT_LINK,
//								linkvideo);
//
//						gotoActivity(PlayVideoYoutbe.class, bundle);
						Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.youtube.com/watch?v=" +linkvideo));
						startActivity(intent);
					}
				} else {
//					Bundle bundle = new Bundle();
//					bundle.putString(E4KidsConfig.KEY_INTENT_VID, vid);
//					bundle.putString(E4KidsConfig.KEY_INTENT_LINK, linkvideo);
//
//					gotoActivity(PlayVideoYoutbe.class, bundle);
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.youtube.com/watch?v=" +linkvideo));
					startActivity(intent);
				}

			}
		});

		lvVideo.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView arg0, int arg1) {

			}

			@Override
			public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
				if ((arg1 + arg2) == arg3 && isLoading) {
					isLoading = false;
					page = page + 1;
					llLoadingFooter.setVisibility(View.VISIBLE);
					loadMore();

					Debug.debug("Page New: " + page);
				}
			}
		});
	}

	private class RequestVideoNew extends BaseRequestHandler {
		public RequestVideoNew(Activity context, OnClickListener clickTry) {
			super(context, clickTry);
		}

		@Override
		public void onWait(BzydroidHttpClient arg0) {
			super.onWait(arg0);
			// llLoading.setVisibility(View.VISIBLE);
		}

		@Override
		public void onStarted(BzydroidHttpClient arg0) {
			super.onStarted(arg0);
			Debug.debug("onStarted");
		}

		@Override
		public void onSuccess(BzydroidHttpClient arg0, NetworkResponse arg1) {

			// Debug.debug("Danh sach : " + a);
			HttpJSONObjectResponse jsonObjectResponse = (HttpJSONObjectResponse) arg0
					.getResponse();
			try {
				JSONObject object = jsonObjectResponse.toJSONObject();
				//
				Debug.debug("Danh sach : " + object.toString());

				JSONObject jObjData = object
						.getJSONObject(E4KidsConfig.KEY_DATA);

				int code = jObjData.getInt(E4KidsConfig.KEY_CODE);

				if (code == 1) {
					Debug.debug("Con du lieu");

					isLoading = true;

					JSONArray jArrVideo = jObjData
							.getJSONArray(E4KidsConfig.ListViewNew.KEY_VIDEO);
					ArrayList<BaseObject> ojsilde = JsonSupport
					.jsonArray2BaseOj(jArrVideo);
					DbSupport.saveCache(ojsilde, TableDb.NEW);
					adapter.setData(DbSupport.convertOj(ojsilde));
//					int length = jArrVideo.length();
//
//					for (int i = 0; i < length; i++) {
//						JSONObject jObj = jArrVideo.getJSONObject(i);
//
//						InfoVideo infoVideo = parseJsonVideo(jObj);
//
//						adapter.addData(infoVideo);
//					}
					lvVideo.setVisibility(View.VISIBLE);
				} else {

					Debug.debug("Het du lieu");
					isLoading = false;
					if (adapter.getCount() <= 0) {
						tvNoData.setVisibility(View.VISIBLE);
						lvVideo.setVisibility(View.VISIBLE);
						llLoadingFooter.setVisibility(View.GONE);
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onError(BzydroidHttpClient arg0,
				BzydroidNetworkExeception arg1) {
			super.onError(arg0, arg1);
			Debug.debug("onError");

			if (!Util.isConnectingToInternet(getActivity())) {
				lvVideo.setVisibility(View.VISIBLE);
				if (llLoading.getVisibility() == View.VISIBLE) {
					llLoading.setVisibility(View.GONE);
				}
			} else {
				lvVideo.setVisibility(View.VISIBLE);
			}
		}

		@Override
		public void onFinished(BzydroidHttpClient arg0) {
			super.onFinished(arg0);
			Debug.debug("onFinished");
			llLoading.setVisibility(View.GONE);
			llLoadingFooter.setVisibility(View.GONE);
		}
	}

	private void loadMore() {
		if (requestVideoNew == null) {
			requestVideoNew = new RequestVideoNew(getActivity(), clickTry);
		}
		RequestService.rqListNew(idCat, String.valueOf(page), packageName,
				requestVideoNew);
	}

	private OnClickListener clickTry = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Debug.debug("clickTry Page : " + page);

			loadMore();
		}
	};
}
